<template>
  <view class="my-container">
    <!-- 用户未登录 -->
    <block v-if="!token">
      <image
        src="/static/images/default-avatar.png"
        class="avatar avarar-img"
      />
      <view class="login-desc">登陆后同步数据</view>
      <!-- #ifdef MP-WEIXIN -->
      <button class="login-btn" type="primary" @click="getUserInfo">
        微信用户一键登录
      </button>
      <!-- #endif -->
      <!-- #ifndef MP-WEIXIN -->
      <button class="login-btn" type="primary" @click="onAutoLogin">
        一键登录
      </button>
      <!-- #endif -->
    </block>
    <!-- 用户已登陆 -->
    <block v-else>
      <image :src="userInfo.avatarUrl" class="avatar avarar-img" />
      <view class="login-desc">{{ userInfo.nickName }}</view>
      <button class="login-btn" type="default" @click="onLogoutClick">
        退出登录
      </button>
    </block>
  </view>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  name: "my-login",
  data() {
    return {};
  },
  methods: {
    ...mapActions("user", ["login", "logout"]),
    /**
     * 非微信端一键登录
     */
    async onAutoLogin() {
      // 展示加载框
      uni.showLoading({
        title: "加载中",
      });
      await this.login({
        encryptedData: "BmGEMqpGI5w",
        errMsg: "getUserProfile:ok",
        iv: "c+NbINO4CuEWCBYGG2FxWw==",
        rawData:
          '{"nickName":"小慕同学","gender":1,"language":"zh_CN","city":"","province":"","country":"China","avatarUrl":"https://m.imooc.com/static/wap/static/common/img/logo-small@2x.png"}',
        signature: "449a10f11998daf680fe546a5176e6e2973516ce",
        userInfo: {
          nickName: "小慕同学",
          gender: 1,
          language: "zh_CN",
          city: "",
          province: "",
        },
      });
      this.$emit("onLoginSuccess");
      // 隐藏loading
      uni.hideLoading();
    },
    /**
     * 微信一键登录
     */
    getUserInfo() {
      uni.showLoading({
        title: "加载中",
      });
      // 获取用户信息，调用login接口
      uni.getUserProfile({
        desc: "登陆后可同步数据",
        success: async (result) => {
          await this.login(result);
          // 登录成功，发送事件
          this.$emit("onLoginSuccess");
        },
        fail: (res) => {
          uni.showToast({
            title: "授权已取消",
            icon: "eror",
            mask: true,
          });
        },
        complate: () => {
          // 异步加载关闭
          uni.hideLoading();
        },
      });
    },
    onLogoutClick() {
      uni.showModal({
        title: "提示",
        content: "确定要退出登录吗？",
        success: ({ confirm, cancel }) => {
          if (confirm) {
            this.logout();
          }
        },
      });
    },
  },
  computed: {
    ...mapState("user", ["userInfo", "token"]),
  },
};
</script>

<style lang="scss" scoped>
.my-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 25%;

  .avarar-img {
    width: 78px;
    height: 78px;
  }

  .login-desc {
    color: #999;
    font-size: 14px;
    margin-top: 16px;
  }

  .login-btn {
    margin-top: 16px;
    width: 85%;
  }
}
</style>
